package com.cn.wanxi.servlet;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/back/login.html").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String code = (String) req.getSession().getAttribute("code");
        String codeipt = req.getParameter("codeipt");
        System.out.println("codeipt:"+codeipt);
        int result = 0;
        UserModel userModel = null;
        if (code.toLowerCase().equals(codeipt.toLowerCase())) {
            userModel = new UserModel();
            userModel.setUsername(req.getParameter("username"));
            userModel.setPassword(req.getParameter("password"));
            IUserService userService = new UserServiceImpl();
            userModel = userService.login(userModel);
        }
        System.out.println(userModel.toString());
        if (userModel!=null)
            result = 1;
        resp.getWriter().println(result);

    }
}
